package com.startdima.user.interfaces;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.startdima.user.application.service.UserService;
import com.startdima.user.infrastructure.dto.SddmResult;
import com.startdima.user.infrastructure.vo.LoginVo;
import com.startdima.user.infrastructure.vo.UserInfoAddOrUpdateVo;
import com.startdima.user.infrastructure.vo.UserInfoVo;
import com.startdima.user.infrastructure.vo.UserQueryVo;

import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping(value = "/app", headers = { "Accept=application/json" })
@Slf4j
@Validated
public class AppController {
 
    
    @Autowired
    private UserService userService;
 
    @PostMapping(value = "/user/login")
    public SddmResult login(@RequestBody @Validated LoginVo vo, BindingResult bindingResult, HttpServletRequest request, 
    		HttpServletResponse response) {
        log.info("---login start---vo:{}", vo.toString());
        return userService.loginFromApp(vo, response);
    }
 
    @PostMapping(value = "/user/test")
    public SddmResult info(HttpServletRequest request, HttpServletResponse response) {
        log.info("---info start---");
        return new SddmResult();
    }
    
    @PostMapping(value = "/user")
    public SddmResult add(@RequestBody @Validated UserInfoAddOrUpdateVo addUser) {
        log.info("---add User---");
        UserInfoVo userInfo = userService.save(addUser);
        return new SddmResult(userInfo);
    }
    
    @PutMapping(value = "/user/{userId}")
    public SddmResult update(@PathVariable Long userId, @RequestBody @Validated UserInfoAddOrUpdateVo updateInfo) {
        log.info("---update User---");
        UserInfoVo userInfo = userService.update(userId, updateInfo);
        return new SddmResult(userInfo);
    }
    
    @DeleteMapping(value = "/user/{userId}")
    public SddmResult delete(@PathVariable Long userId) {
        log.info("---delete User---");
        userService.delete(userId);
        return new SddmResult();
    }
    
    @GetMapping(value = "/user/list")
    public SddmResult search(@ModelAttribute UserQueryVo queryVo) {
        log.info("---search User---");
        Page<UserInfoVo> resultPage = userService.search(queryVo);
        return new SddmResult(resultPage);
    }

}
